package cn.fuxi2022.core.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池的工具类
 */
@Slf4j
public class ExecutortUtils {

    private static Integer code = Runtime.getRuntime().availableProcessors();

    private static ThreadPoolExecutor threadPoolExecutor = null;

    static {
        log.info("线程池初始化");
        threadPoolExecutor = new ThreadPoolExecutor(
                code, // 核心线程数(线程池中始稳定的线程最小数量)
                code * 2, // 最大线程数量
                3, // 空闲线程时间
                TimeUnit.SECONDS, // 时间单位
                new LinkedBlockingQueue<>(1000), // 队列
                Executors.defaultThreadFactory(), // 线程创建工厂
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
        );
    }

    public static ThreadPoolExecutor getThreadPool() {
        return threadPoolExecutor;
    }

}